﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;

namespace Hi_Baidu.Remote
{
    public class RemoteConnect
    {
        /// <summary>
        ///     注册用来连接对方的远程通道
        /// </summary>
        /// <param name="ChannelProtocol" type="RemoteConnect.RemoteConnect.RemoteProtocol">
        ///     <para>
        ///         通道使用的协议
        ///     </para>
        /// </param>
        public static void RegisterChannel(RemoteProtocol ChannelProtocol)
        {
            IChannel channel = null;
            switch (ChannelProtocol)
            {
                case RemoteProtocol.TCP:
                    channel = new TcpChannel(0);
                    break;
                case RemoteProtocol.UDP:
                    break;
                case RemoteProtocol.HTTP:
                    break;
                case RemoteProtocol.SMTP:
                    break;
                default:
                    break;
            }
            ChannelServices.RegisterChannel(channel);
        }

        /// <summary>
        ///     根据配置文件创建通道, 并且注册远程对象
        /// </summary>
        /// <param name="ConfigurationFilePath" type="string">
        ///     <para>
        ///         配置文件路径
        ///     </para>
        /// </param>
        public static void RegisterChannel(String ConfigurationFilePath)
        {
            RemotingConfiguration.Configure(ConfigurationFilePath);
        }

        /// <summary>
        ///     远程连接类别
        /// </summary>
        public enum RemoteProtocol
        {
            /// <summary>
            ///     TCP/IP 协议
            /// </summary>
            TCP,
            /// <summary>
            ///     UDP 协议
            /// </summary>
            UDP,
            /// <summary>
            ///     HTTP 协议
            /// </summary>
            HTTP,
            /// <summary>
            ///     SMTP 协议
            /// </summary>
            SMTP
        }

        /// <summary>
        ///     得到远端指定的远程对象引用
        /// </summary>
        /// <param name="type" type="System.Type">
        ///     <para>
        ///         要获得的远程对象引用类型
        ///     </para>
        /// </param>
        /// <param name="Url" type="string">
        ///     <para>
        ///         获取通道的URL
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回获取后的对象
        /// </returns>
        public static Object Connect(Type type, String Url)
        {
            return Activator.GetObject(type, Url);
        }
    }
}
